package com.overlays;

import java.util.List;

import android.graphics.Canvas;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Point;

import com.driving.Route;
import com.driving.DrivingDirections.Mode;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

public class MyOverlay extends Overlay {
	private Route route;
	private Mode mode;
	
	
	public Route getRoute() {
		return route;
	}


	public void setRoute(Route route, Mode mode) {
					this.route = route;
		this.mode = mode;
	}


	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
		// TODO Auto-generated method stub
		super.draw(canvas, mapView, shadow);
		if (route != null) {
			drawRoute (route.getGeoPoints(), canvas, mapView);
			/*if (startIcon != 0 && middleIcon != 0 && endIcon != 0) {
				drawPlacemarks (route.getPlacemarks(), canvas, mapView);
				}*/
			}
	}
	
	private void drawRoute(List geoPoints, Canvas canvas, MapView mapView)
	{
	for (int i = 0; i < geoPoints.size() - 1; i++)
	{
	// Convert the starting geographical point coordinates to screen coordinates.
	//
	GeoPoint startGeoPoint = (GeoPoint) geoPoints.get (i);
	Point startScreenPoint = new Point ();
	mapView.getProjection().toPixels(startGeoPoint, startScreenPoint);

	// Convert the ending geographical point coordinates to screen coordinates.
	//
	GeoPoint endGeoPoint = (GeoPoint) geoPoints.get (i + 1);
	Point endScreenPoint = new Point ();
	mapView.getProjection().toPixels(endGeoPoint, endScreenPoint);

	// Draw a joining line between the starting and the ending points.
	//
	Paint paint = new Paint();
	paint.setStrokeWidth(5);
	paint.setAntiAlias(true);
	if ((mode != null) && (mode == Mode.DRIVING)) {
	paint.setARGB(64, 0, 0, 255);
	}
	else {
	paint.setARGB(96, 0, 0, 0);
	paint.setPathEffect(new DashPathEffect(new float[]{8, 4}, 1));
	}
	canvas.drawLine(startScreenPoint.x, startScreenPoint.y, endScreenPoint.x, endScreenPoint.y, paint);
	
	}
}
}
